(function(KKBus){

    function DefectController() {
        var self = this;
        
     
        self.defect = ko.observableArray();
        
           self.getDefect= function(index) {
               
            self.defect.removeAll();
            app.request++;
            
            var auto = app.Knockout.BusController.bus()[index];
            var autoID = auto.id();
            
            var liczba = autoID;    
            console.log(liczba);
            var url = 'rest/bus/'+liczba+'/defect/';
                     
            app.request++;
                       
            
                               
                
        KKBus.Ajax.doGet(url, function(data, textStatus, jqXHR)
        {
      
        var jsData = JSON.parse(data);
        var i = jsData.length;
        
        
            while(i--){
                 self.defect.push(new KKBus.DefectModel(jsData[i].description,jsData[i].cost, jsData[i].busId)); 
                     }
                 app.request--;
                  
          if (app.request == 0) {
                    if (app.settings.DEBUG == true) { console.log("Load template from: " + DefectController.name); }
                    setTimeout(function() { app.loadTemplate(); }, 1000);
                }
            });
            
            };
           
           self.validation = ko.observable(false);
           self.errorMessage = ko.observable("Blad");
           self.success = ko.observable(false);
            
           self.addDefect = function() {
                   
                              
                     
             var url = 'rest/defect/';
                 
             var defect = JSON.stringify({
                    busId:$('#formBusId').val(),
                    description: $('#formDescription').val(),
                    cost: $('#formCost').val(),
                    date: "2012-12-12"
                });
                
                if(KKBus.Validation.length($('#formBusId').val())
                    && KKBus.Validation.length($('#formDescription').val())
                    && KKBus.Validation.length($('#formCost').val())){  
                
                    if(KKBus.Validation.word($('#formDescription').val())){
                    
                       if(KKBus.Validation.koszt($('#formCost').val())){
                        
                    
             
                             KKBus.Ajax.doPost(url, defect, function(result, textStatus, jqXHR) {
                             if (result == "ADDED") {
                                    self.success(true);
                                    $('#formBusId').val(' ');
                                    $('#formDescription').val(' ');
                                    $('#formCost').val(' ');
                                    setTimeout(function() 
                                    {   
                                        
                                        self.success(false);
                                        }, 3000);
                                        
                                          var temp = JSON.parse(defect);

                                          var DefectAdd = new KKBus.DefectModel(temp.description,temp.cost,temp.busId);

                                          self.defect.push(DefectAdd);                                                                 
                                    
                                 }
                               });
                               
                       }else{
                           
                           self.validation(true);
                           self.errorMessage("Błednie wprowadzony koszt naprawy!");
                           setTimeout(function() { self.validation(false); }, 3000);
                         
                       }; 
                       
                    }else{
                        
                        self.validation(true);
                        self.errorMessage("Błedny opis awarii!");
                        setTimeout(function() { self.validation(false); }, 3000);
                        
                    };
                    
                   }else{
                       
                      self.validation(true);
                      self.errorMessage("Puste pola!");
                      setTimeout(function() { self.validation(false); }, 3000);
                       
                   };
                       
               };
  

    }
    KKBus. DefectController =  DefectController;
    
}(window.KKBus));

